package io.renren.project.demo2.controller;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import io.renren.common.utils.R;
import io.renren.modules.wyzsphone.utils.Utils;
import io.renren.project.demo2.service.Demo2Service;
import io.renren.project.hongchang.entity.AddCar;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/demo2")
public class Demo2Controller {

    @Autowired
    Demo2Service Demo2Service;

    @RequestMapping("/getAllOrder")
    public R getAllOrder(@RequestBody String jsonStr){
        Map<String, Object> params = Utils.JsonToMap(jsonStr);
        Map o = Demo2Service.getAllOrder(params);
        return R.ok().put("data",o);
    }

    //暂时没用到
    @RequestMapping("/getAllSalesCarByorder")
    public R getAllSalesCarByorder(@RequestBody String jsonStr){
        Map<String, Object> params = Utils.JsonToMap(jsonStr);
        Map o = Demo2Service.getAllSalesCarByorder(params);
        return R.ok().put("data", o);
    }

    @RequestMapping("/getAppOrderList")
    public R getAppOrderList(@RequestBody String jsonStr)
    {
        Map<String,Object> params = Utils.JsonToMap(jsonStr);
        Map o = Demo2Service.getAppOrderList(params);
        if("2".equals(o.get("state"))){
            return  R.uidError("uid过期");
        } else{
            return R.ok().put("data",o);
        }
    }

    @RequestMapping("/getSalesCarList")
    public R getSalesCarList(@RequestBody String jsonStr)
    {
        Map<String,Object> params = Utils.JsonToMap(jsonStr);
        Map o = Demo2Service.getSalesCarList(params);
        if("2".equals(o.get("state"))){
            return R.uidError("uid过期");
        }
        else{
            return R.ok().put("data",o);
        }
    }

    @RequestMapping("/getQtySumByItem")
    public R getQtySumByItem(@RequestBody String jsonStr){
        Map<String,Object> params = Utils.JsonToMap(jsonStr);
        Map o = Demo2Service.getQtySumByItem(params);
        if("2".equals(o.get("state"))){
            return R.uidError("uid过期");
        }
        else{
            return R.ok().put("data",o);
        }
    }

    @RequestMapping("/getNumber")
    public R getSalesOrder(@RequestBody String jsonStr){
        List<String> list = new ArrayList<>();
        for(int i = 0; i<11; i++){
            String uuid = UUID.randomUUID().toString();
            String s = uuid.replace("-","");
            list.add(s);
        }

        return R.ok().put("data",list);
    }

    @RequestMapping("/getSalesOrderByOrderID")
    public R getSalesOrderByOrderID(@RequestBody String jsonStr)
    {
        Map<String, Object> params = Utils.JsonToMap(jsonStr);
        Map o = Demo2Service.getSalesOrderListByOrderID(params);
        return R.ok().put("data", o);
    }

    @RequestMapping("/saveBill")
    public R saveBill(@RequestBody String jsonStr){
        Gson _gson = new Gson();
        Map<String, Object> params = _gson.fromJson(jsonStr, new TypeToken<Map<String, Object>>(){}.getType());
        Map o = Demo2Service.saveBill(params);
        if("2".equals(o.get("state"))){
            return R.uidError("uid过期");
        }else if("1".equals((o.get("state")))){
            return  R.error(1,"操作失败"+o.get("msg"));
        }else{
           // String str = "操作成功"+o.get("Message");
            return R.ok().put("data", o);
        }
    }

    @RequestMapping("/AppCarListAdd")
    public R AppCarListAdd(@RequestBody String jsonStr)
    {
        Map<String,Object> params = Utils.JsonToMap(jsonStr);
        Gson _gson = new Gson();
        AddCar addCar =  _gson.fromJson(jsonStr, AddCar.class);
        Map o= Demo2Service.AppCarListAdd(params,jsonStr);
        if("2".equals(o.get("state"))){
            return R.uidError("uid过期");
        }
        else if("1".equals(o.get("state"))){
            return R.error(1,"操作失败");
        }else{
            return R.ok().put("data", o);
        }
    }

    @RequestMapping("/AppCarListDel")
    public R AppCarListDel(@RequestBody String jsonStr){
        Map<String, Object> params = Utils.JsonToMap(jsonStr);
        Gson _gson = new Gson();
        Map o = Demo2Service.AppCarListDel(params, jsonStr);
        if("2".equals(o.get("state"))){
            return R.uidError("uid过期");
        }else if("1".equals(o.get("state"))){
            return R.error(1,"操作失败");
        }else{
            return R.ok().put("data", o);
        }
    }



    //获取批量下单详情
    @RequestMapping("/getAppOrderBill")
    public R getAppOrderBill(@RequestBody String jsonStr)
    {
        Map<String,Object> params = Utils.JsonToMap(jsonStr);
        Map o= Demo2Service.getAppOrderBill(params);
        if ("2".equals(o.get("state"))) {
            return R.uidError("uid过期");
        } else {

            return R.ok().put("data", o);
        }
    }

    @RequestMapping("/GetAppOrderCarInfo")
    public R getAppOrderCarInfo(@RequestBody String jsonStr)
    {
        Map<String,Object> params = Utils.JsonToMap(jsonStr);
        Map o = Demo2Service.getAppOrderCarInfo(params);
        if("2".equals(o.get("state"))){
            return R.uidError("uid过期");
        }else{
            return R.ok().put("data", o);
        }
    }

}
